package com.zrrd.gmall.webAll.controller;

import com.zrrd.gmall.common.result.Result;

import com.zrrd.gmall.list.feign.ListFeignClient;
import com.zrrd.gmall.model.list.SearchParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model){
        Result<Map> result = listFeignClient.list(searchParam);
        System.out.println("搜索出的结果是=======>"+result.getData());
        model.addAllAttributes(result.getData());
        //存入拼接好的url
        model.addAttribute("urlParam",this.makeUrlParam(searchParam));

        model.addAttribute("orderMap",this.makeOrderMap(searchParam.getOrder()));
        //存入品牌回显
        model.addAttribute("trademarkParam",this.makeTrademark(searchParam.getTrademark()));
        //存入平台条件回显
        model.addAttribute("propsParamList",this.makeProps(searchParam.getProps()));
        return "/list/index";
    }
    //制作前端需要的参数拼接URL字符串
    private String makeUrlParam(SearchParam searchParam){
        //创建字符串缓存区
        StringBuilder builder = new StringBuilder();
        //关键字
        if (searchParam.getKeyword()!=null){
            builder.append("keyword=").append(searchParam.getKeyword());
        }
        if (searchParam.getCategory1Id()!=null){
            builder.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id()!=null){
            builder.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id()!=null){
            builder.append("category3Id=").append(searchParam.getCategory3Id());
        }
        // 处理品牌
        if(searchParam.getTrademark()!=null){
                builder.append("&trademark=").append(searchParam.getTrademark());
        }
        // 判断平台属性值
        if (null != searchParam.getProps()){
            for (String prop : searchParam.getProps()) {
                if (prop.length() > 0){
                    builder.append("&props=").append(prop);
                }
            }
        }
        return "list.html?" + builder.toString();
    }
    private Map<String,Object> makeOrderMap(String order){
        Map<String,Object> map =new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split!=null && split.length==2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        }else {
            map.put("type","1");
            map.put("sort","asc");
        }
        return map;
    }
    //前端需要的回显字符串
    private String makeTrademark(String trademark){
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = StringUtils.split(trademark, ":");
            if (split != null && split.length == 2) {
                return "品牌：" + split[1];
            }
        }
        return "";
    }
    private List<Map<String,String>> makeProps(String[] props){
        List<Map<String,String>> list = new ArrayList<>();
        if (props != null && props.length>0){
            for (String prop : props){
                String[] split = prop.split(":");
                if (split!=null && split.length ==3){
                    Map<String,String> map = new HashMap<>();
                    map.put("attrId",split[0]);
                    map.put("attrValue",split[1]);
                    map.put("attrName",split[2]);
                    list.add(map);
                }
            }
        }
        return list;
    }
}
